package com.swdegao.iot.netty.utils;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class ConvertUtils {
	public static byte[] DTU_TYPE5 = { 0x60, 0x61, 0x62, 0x63, 0x64 };
	
	/**
	 * 字节数据转16进制字符串
	 * @param data
	 * @return
	 */
	public static String printHexBinary(byte[] data) {
		char[] hexCode = "0123456789ABCDEF".toCharArray();
		int total = data.length * 2;
		StringBuilder r = new StringBuilder(total);
		for (byte b : data) {
			r.append(hexCode[(b >> 4) & 0x0F]);
			r.append(hexCode[(b & 0x0F)]);
		}
		return r.toString();

	}
	
	public static byte getTotalForByte(byte[] bit) {
		int sum = 0;
		if (bit.length > 0) {
			for (int i = 0; i < bit.length; i++) {
				sum = sum + bit[i];
				sum = sum & 0xFF;
			}
		}
		byte rein = (byte) sum;
		return rein;
	}

	public static byte[] getByte(int ad, int ws) {
		byte[] bit = new byte[4];
		String adtemp = String.valueOf(ad);
		int length = ws * 2 - adtemp.length();
		for (int i = 0; i < length; i++) {
			adtemp = "0" + adtemp;
		}
		for (int i = 0; i < ws; i++) {
			int index = i * 2;
			String temp = adtemp.substring(index, index+2);
			bit[i] = (byte) Integer.parseInt(temp,16);
		}
		return bit;
	}

	public static byte[] getDateFromStr(String str, int ws) {
		byte[] bit = new byte[ws];
		if (str.length() > 0) {
			int k = 0;
			for (int i = ws; i > 0; i--) {
				int food = i * 2 - 1;
				int head = food - 1 < 0 ? 0 : food - 1; 
				bit[k] = (byte) Integer.parseInt(str.substring(head, head+2),16);
				k++;
			}
		}
		return bit;
	}
	public static byte[] getDateFromStrDX(String str, int ws) {
		byte[] bit = new byte[ws];
		if (str.length() > 0) {
			int k = ws - 1;
			for (int i = ws; i > 0; i--) {
				int food = i * 2 - 1;
				int head = food - 1 < 0 ? 0 : food - 1;
				bit[k] = (byte) Integer.parseInt(str.substring(head, head+2),16);
				k--;
			}
		}
		return bit;
	}

	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2); // 除以2是因为十六进制比如a1使用两个字符代表一个byte
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			// 乘以2是因为十六进制比如a1使用两个字符代表一个byte,pos代表的是数组的位置
			// 第一个16进制数的起始位置是0第二个是2以此类推
			int pos = i * 2;
			// <<4位就是乘以16 比如说十六进制的"11",在这里也就是1*16|1,而其中的"|"或运算就相当于十进制中的加法运算
			// 如00010000|00000001结果就是00010001
			// 而00010000就有点类似于十进制中10而00000001相当于十进制中的1，与是其中的或运算就相当于是10+1(此处说法可能不太对，)
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}
	 private static byte toByte(char c)
     {
         byte b = (byte)"0123456789ABCDEF".indexOf(c);
         return b;
     }
	public static byte getByte(char c) {
		Charset cs = Charset.forName("UTF-8");
		CharBuffer cb = CharBuffer.allocate(1);
		cb.put(c);
		cb.flip();
		ByteBuffer bb = cs.encode(cb);
		byte[] tmp = bb.array();
		return tmp[0];
	}

	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
//		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		int length = hexChars.length;
		byte[] d = new byte[length];
		if(length==1) {
			d[0] = (byte) (charToByte(hexChars[0]) << 4);
		}else {
			for (int i = 0; i < length; i++) {
				int pos = i * 2;
				d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
			}
		}
		return d;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

}
